package com.example.administrator.taxicar.ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.example.administrator.taxicar.R;
import com.example.administrator.taxicar.utils.Constants;

public class BaiduMapFragment extends Fragment {

    public MyLocationListenner myListener = new MyLocationListenner();
    // 定位相关
    LocationClient mLocClient;
    BitmapDescriptor mCurrentMarker_new = BitmapDescriptorFactory.fromResource(R.drawable.navidibiao);
    BitmapDescriptor mCurrentMarker_car1 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_fast);
    BitmapDescriptor mCurrentMarker_car2 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_driver_car2);
    BitmapDescriptor mCurrentMarker_car3 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_fast);
    BitmapDescriptor mCurrentMarker_car4 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_fast2);
    BitmapDescriptor mCurrentMarker_car5 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_fast3);
    BitmapDescriptor mCurrentMarker_car6 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_fast4);
    BitmapDescriptor mCurrentMarker_car7 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_luxury);
    BitmapDescriptor mCurrentMarker_car8 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_tailwind2);
    BitmapDescriptor mCurrentMarker_car9 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_tailwind3);
    BitmapDescriptor mCurrentMarker_car10 = BitmapDescriptorFactory.fromResource(R.drawable.map_icon_tailwind4);
    BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.car_icon_webview_refresh);
    MapView mMapView;
    BaiduMap mBaiduMap;
    // UI相关
    boolean isFirstLoc = true; // 是否首次定位
    private MyLocationConfiguration.LocationMode mCurrentMode;

    private InfoWindow mInfoWindow;

    private double jing;
    private double wei;
    Marker mMarkerA;

    public BaiduMapFragment() {
    }

    public static BaiduMapFragment newInstance() {
        BaiduMapFragment fragment = new BaiduMapFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_map, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;

        // 地图初始化
        mMapView = (MapView) getView().findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        mLocClient = new LocationClient(getActivity());
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setAddrType("all");
        option.setScanSpan(5000);
        mLocClient.setLocOption(option);
        mLocClient.start();

        // 修改为自定义marker
        mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker,
                ContextCompat.getColor(getActivity(), R.color.touming), ContextCompat.getColor(getActivity(), R.color.touming)));
    }

    public void myMaker(Marker maker, double jing, double wei, BitmapDescriptor bitmap) {
        LatLng llA = new LatLng(jing, wei);
        MarkerOptions ooA = new MarkerOptions().position(llA).icon(bitmap).zIndex(9).draggable(true);
        maker = (Marker) (mBaiduMap.addOverlay(ooA));
    }

    public void initOverlay() {
        myMaker(mMarkerA, jing, wei, mCurrentMarker_new);
        //30.6499350000,104.1011240000
        //30.6496240000,104.0999380000
        //30.6485680000,104.0992550000
        myMaker(mMarkerA, 30.64602, 104.100225, mCurrentMarker_car1);
        myMaker(mMarkerA, 30.645865, 104.099686, mCurrentMarker_car2);
        myMaker(mMarkerA, 30.6461450000, 104.1024890000, mCurrentMarker_car3);
        myMaker(mMarkerA, 30.6452750000, 104.1011600000, mCurrentMarker_car4);
        myMaker(mMarkerA, 30.6480400000, 104.0981770000, mCurrentMarker_car5);
        myMaker(mMarkerA, 30.6463310000, 104.0972070000, mCurrentMarker_car6);
        myMaker(mMarkerA, 30.6459270000, 104.0976020000, mCurrentMarker_car7);
        myMaker(mMarkerA, 30.6473560000, 104.0993270000, mCurrentMarker_car8);
        myMaker(mMarkerA, 30.6476360000, 104.1000820000, mCurrentMarker_car9);
        myMaker(mMarkerA, 30.6480400000, 104.1001540000, mCurrentMarker_car10);
        myMaker(mMarkerA, 30.6449640000, 104.0973510000, mCurrentMarker_car1);
        myMaker(mMarkerA, 30.6440630000, 104.0980700000, mCurrentMarker_car5);
        myMaker(mMarkerA, 30.6462070000, 104.0978180000, mCurrentMarker_car3);

    }

    @Override
    public void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 退出时销毁定位
        mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                SharedPreferences adress_str = getActivity().getSharedPreferences(Constants.ADRESS, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = adress_str.edit();

                LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

                String myLocation = location.getAddrStr();
                Log.e("tag", "myLocation:" + myLocation);


                //当前设备位置所在的市
                String city = location.getCity();
                String street = location.getStreet();
                String streetNumber = location.getStreetNumber();
                Log.e("tag", "street:" + street);
                Log.e("tag", "streetNumber:" + streetNumber);

                jing = location.getLatitude();
                wei = location.getLongitude();

                //TODO 给坐标添加个上车地 目的地的指示器
                initOverlay();
                Button button = new Button(getActivity());
                button.setBackgroundResource(R.drawable.popup);
                InfoWindow.OnInfoWindowClickListener listener;

                if (!city.equals("") & !street.equals("") & !streetNumber.equals("")) {
                    button.setText(city + street + streetNumber);
                } else {
                    button.setText("上车地点");
                }
                listener = new InfoWindow.OnInfoWindowClickListener() {
                    public void onInfoWindowClick() {
                    }
                };
                mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), ll, -47, listener);
                mBaiduMap.showInfoWindow(mInfoWindow);

                Log.e("tag", "city:" + city);
                editor.putString(Constants.ADRESS_STR, myLocation);
                editor.putString(Constants.ADRESS_CITY, city);
                editor.commit();
            }
        }
    }
}